package logic

import (
	"cart_srv/model/mysql"
	"context"
	"gorm.io/gorm"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetCartCheckedGoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCartCheckedGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCartCheckedGoodsListLogic {
	return &GetCartCheckedGoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetCartCheckedGoodsListLogic) GetCartCheckedGoodsList(in *cart.GetCartCheckedGoodsListRequest) (*cart.GetCartCheckedGoodsListResponse, error) {
	// todo: add your logic here and delete this line
	c := mysql.Cart{
		Model: gorm.Model{ID: uint(in.UserId)},
	}
	list, err := c.CartList()
	if err != nil {
		return nil, err
	}
	var checkoutGoodsList []*cart.CartGoodsItem
	for _, v := range list {
		goodsItem := cart.CartGoodsItem{
			GoodsId:    v.GoodsID,
			UserId:     int64(v.UserID),
			GoodsTitle: v.GoodsTitle,
			GoodPrice:  v.GoodsPrice,
			GoodNum:    v.GoodsNum,
		}
		checkoutGoodsList = append(checkoutGoodsList, &goodsItem)
	}
	return &cart.GetCartCheckedGoodsListResponse{CartGoodItem: checkoutGoodsList}, nil
}
